#!/usr/bin/env python
"""
Run all analyses for Bank Regulatory Penalties and Corporate Financing Choices
This script executes all steps in sequence:
1. Generate synthetic data
2. Create visualizations
3. Run regression analyses
"""

import os
import subprocess
import time
import pandas as pd

print("银行监管处罚对企业融资选择影响的实证分析\n")

# 创建必要的目录
if not os.path.exists('data'):
    os.makedirs('data')
if not os.path.exists('figures'):
    os.makedirs('figures')
if not os.path.exists('results'):
    os.makedirs('results')

# 步骤1: 生成合成数据
print("步骤1: 生成合成数据")
start_time = time.time()
subprocess.run(['python', 'generate_synthetic_data.py'], check=True)
print(f"数据生成完成，用时 {time.time() - start_time:.2f} 秒.\n")

# 步骤2: 创建可视化
print("步骤2: 创建可视化")
start_time = time.time()
subprocess.run(['python', 'analyze_data.py'], check=True)
print(f"数据分析和可视化完成，用时 {time.time() - start_time:.2f} 秒.\n")

# 步骤3: 运行回归分析
print("步骤3: 运行回归分析")
start_time = time.time()
subprocess.run(['python', 'regression_analysis.py'], check=True)
print(f"回归分析完成，用时 {time.time() - start_time:.2f} 秒.\n")

# 显示生成的表格
print("\n" + "="*50)
print("实证分析章节表格结果:")
print("="*50)

# 显示表1: 基准回归结果
try:
    print("\n表1: 基准回归结果")
    baseline_results = pd.read_csv('results/表1_基准回归结果.csv')
    print(baseline_results.to_string(index=False))
except:
    print("无法读取表1数据")

# 显示表2: 内生性处理
try:
    print("\n\n表2: 内生性处理")
    endogeneity_results = pd.read_csv('results/表2_内生性处理.csv')
    print(endogeneity_results.to_string(index=False))
except:
    print("无法读取表2数据")

# 显示表3: 稳健性检验
try:
    print("\n\n表3: 稳健性检验")
    robustness_results = pd.read_csv('results/表3_稳健性检验.csv')
    print(robustness_results.to_string(index=False))
except:
    print("无法读取表3数据")

# 显示表4: 异质性分析
try:
    print("\n\n表4: 异质性分析")
    heterogeneity_results = pd.read_csv('results/表4_异质性分析.csv')
    print(heterogeneity_results.to_string(index=False))
except:
    print("无法读取表4数据")

print("\n" + "="*50)
print("所有分析已完成。结果保存在 'results' 目录中，图表保存在 'figures' 目录中。")

# List generated files
print("\n" + "="*50)
print("Generated files:")
print("="*50)
for directory in ['data', 'figures', 'results']:
    print(f"\n{directory}:")
    for file in sorted(os.listdir(directory)):
        file_size = os.path.getsize(os.path.join(directory, file)) / 1024  # KB
        print(f"  - {file} ({file_size:.1f} KB)")

print("\n" + "="*50)
print("All analyses completed successfully!")
print("="*50) 